# -*- coding: utf-8 -*-
'''
Created on 2013-5-30
定时器
@author: 不懂d叛逆
'''
import threading, time
class timer(threading.Thread):
    def __init__(self, delay, func, args = ()):
        self.__delay = delay
        threading.Thread.__init__(self)
        self.endless = True
        self.__func = func
        self.__args = args
        self.start()
        return
    def run(self):
        while(self.endless):
            time.sleep(self.__delay)
            if self.endless:
                if len(self.__args) > 0:
                    self.__func(self.__args)
                else:
                    self.__func()
        return
    def stop(self):
        self.endless = False
        self.join(0.1)
        if self._stopped == False:
            self._stop()

class onceTimer(threading.Thread):
    def __init__(self, delay, func, args = ()):
        self.__delay = delay
        threading.Thread.__init__(self)
        self.__func = func
        self.__args = args
        self.start()
        return
    def run(self):
            time.sleep(self.__delay)
            if len(self.__args) > 0:
                self.__func(self.__args)
            else:
                self.__func()
